package com.lyhengtongwl.zqsnews.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.entity.CategoryEntity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 分类的菜单适配器
 */
public class MenuAdapter extends BaseAdapter {

    List<CategoryEntity> mList;
    Context mContext;
    private int mCurrentItem = 0;

    public MenuAdapter(Context mContext, List<CategoryEntity> mList) {
        this.mList = mList;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh = null;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_menu, null);
            vh = new ViewHolder(view);
            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }
        vh.tvMenutxt.setText(mList.get(i).getCategoryTitle());
        if (mCurrentItem == i) {//当前选中项
            vh.rlItemlayout.setBackgroundResource(R.color.white);
            vh.viewFlag.setVisibility(View.VISIBLE);
        } else {
            vh.rlItemlayout.setBackgroundResource(R.color.colorEEE);
            vh.viewFlag.setVisibility(View.GONE);
        }
        return view;
    }

    static class ViewHolder {
        @BindView(R.id.rl_itemlayout)
        RelativeLayout rlItemlayout;
        @BindView(R.id.tv_menutxt)
        TextView tvMenutxt;
        @BindView(R.id.view_flag)
        View viewFlag;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    /*
    设置选中项背景颜色
     */
    public void setSelectColor(int position) {
        this.mCurrentItem = position;
    }

    public int getSelectPosition() {
        return mCurrentItem;
    }

}
